 // 获取应用实例
const app = getApp();
let urls = require("../../utils/util");
const api = require("../../utils/api");
Page({
  data: {
    commodity:"",
    curC:[],
    // 商品的ID
    productId:[],
    // se:1
    // 结算按钮显示数值
    jsNum:"",
    money:"",
    //全选按钮
    curAll:1,
  },
  // 圆圈选择
  // xuanze(){
  //   let that = this;
  //   this.setData({
  //     se:that.data.se==1?0:1
  //   })
  // },
  // 圆圈选择
  onTap(e){
    console.log(e);
    let that = this;
    let totalmoney=0;
    let index = e.currentTarget.dataset.index;
    let curVal = that.data.curC[index];
    this.setData({
      ["curC[" + index + "]"]:!curVal?1:0
    })
    let temArr =[];
    //只要有一个商品被选中就全选按钮是灰色
    this.data.curC.some(function(cur,index){
      if(cur==0){
        that.setData({
          curAll:1
        })
      }
    })
    //全部商品被选中，全选按钮是红色
    this.data.curC.every(function(cur,index){
      if(cur==1){
        that.setData({
          curAll:0
        })
      }
    })
    for(var i=0;i<this.data.curC.length;i++){
      if(this.data.curC[i]==1){
        temArr.push(this.data.commodity[i].product_id);
        totalmoney+=this.data.commodity[i] .market_price*this.data.commodity[i].number;
      }
    };
    that.setData({
      productId:temArr
    });
    this.setData({
      money:totalmoney,
      jsNum:"("+(temArr.length)+")"
    })
  },
  //全选按钮
  toAll(e){
    let totalmoney=0;
    let temArr =[];
    this.setData({
      curAll:this.data.curAll==1?0:1
    })
    for(var i=0;i<this.data.curC.length;i++){
      if(this.data.curC[i]==0){
       totalmoney+=this.data.commodity[i] .market_price*this.data.commodity[i].number;
       temArr.push(this.data.commodity[i].product_id);
        this.setData({
          ["curC["+i+"]"]:1,
        })
      }else{
        temArr=[];
        this.setData({
          ["curC["+i+"]"]:0
        })
      }
    }
    this.setData({
      money:totalmoney,
      jsNum:"("+(temArr.length)+")"
    })

  },
  // 添加按钮
  add(e){
    let that = this;
    let number = e.currentTarget.dataset.num;
    let goodsId = e.currentTarget.dataset.goodsid;
    let productId = e.currentTarget.dataset.productid;
    let id = e.currentTarget.dataset.id;
    number++;
    urls.merReq(api.updateshopcarUrl,{
      goodsId:goodsId.toString(),
      number:number,
      productId:productId,
      id:id
    },"POST",res=>{
      that.setData({
        commodity:res.data.data.cartList
      })
    })
  },
  //实例化对象的__proto__指向了构造函数的prototype,obj.__proto__ == Obj.prototype
  sub(e){
    let that = this;
    let number = e.currentTarget.dataset.num;
    let goodsId = e.currentTarget.dataset.goodsid;
    let productId = e.currentTarget.dataset.productid;
    let id = e.currentTarget.dataset.id;
    number--;
    urls.merReq(api.updateshopcarUrl,{
      goodsId:goodsId.toString(),
      number:number,
      productId:productId,
      id:id
    },"POST",res=>{
      that.setData({
        commodity:res.data.data.cartList
      })
    })
  },
  // 删除购物车的商品
  del(e){
    urls.merReq(api.delshopcarUrl,{
      productIds:e.currentTarget.id
    },res=>{
      console.log(res);
      this.setData({
        commodity:res.data.data.cartList,
      })
      }, "POST")
  },
  //结算购物车被选中的商品
  count(e){
    let isChecked;
    if(this.data.productId.length>0){
      isChecked=1;
      urls.req(api.count,{
        productIds:this.data.productId.toString(),
        isChecked:isChecked
      },"POST",res=>{
        this.setData({
          productId:[],
          curAll:1
        })
        console.log(res);
        wx.navigateTo({
          url: '../../component/submit/submit'
        })
      })
    }else{
      isChecked = 0;
      wx.showToast({
        title:'请选择商品',
        icon:"none",
        mask:true
      })
    }
  },
  onShow: function(){
    let that = this;
    wx.showNavigationBarLoading();
    wx.showLoading({
      title: "正在加载",
      mask: true
    });
    urls.merReq(api.shopcarUrl,{},res=>{
      wx.hideNavigationBarLoading();
      wx.hideLoading();
      // 将请求到的数据储存到本地
      wx.setStorage({
        key:"shopcar",
        data:res.data.data.cartList
      })
      // 对data里面的commodity赋值
      that.setData({
        commodity:res.data.data.cartList
      })
      res.data.data.cartList.forEach((cur,index) => {
        that.setData({
          ["curC["+index+"]"]:0
        })
      });
    }, "GET")
  }
})